package com.cvmars.jianyou.model;

import android.text.TextUtils;
import android.util.Log;

import com.cvmars.jianyou.api.model.UserMode;
import com.cvmars.jianyou.config.MyConfig;
import com.cvmars.jianyou.utils.LogUtils;
import com.orhanobut.hawk.Hawk;

/**
 * Created by hehaifeng on 2018/8/9.
 */

public class UserInfoManager {


    public static String USER_CACHE = "userModel";
    private static String IS_USER_LOGIN = "isUserLogin";
    private static String USER_TOKEN = "userToken";

    private static Boolean isUserLogin = false;
    private static UserInfoManager INSTANCE = new UserInfoManager();
    private static UserMode model;
    private static String userToken;

    public static String uid;
    public static boolean  is_rut;

    private UserInfoManager() {
    }

    public static UserInfoManager getInstance() {

        model = Hawk.get(USER_CACHE);
        userToken = Hawk.get(USER_TOKEN);
        return INSTANCE;
    }

    public UserMode getUserModel() {

        return model;
    }


    /*
    * 判断是否自己
    *
    * */
    public boolean isOneSelf(String lid) {

        boolean isSelf = false;
        if (!TextUtils.isEmpty(lid) && lid.equals(uid)) {
            isSelf = true;
        }
        return isSelf;
    }


    /**
     * 返回用户token
     *
     * @return
     */
    public String getUserToken() {

        if (userToken == null) {
            userToken = Hawk.get(USER_TOKEN);
        }
        return userToken;
    }

    /**
     * 登陆操作
     *
     * @param model
     */
    public void onLogin(UserMode model) {

        if (model == null) {
            return;
        }
        this.model = model;
        userToken = model.token;
        isUserLogin = true;
        Hawk.put(USER_CACHE, model);
        Hawk.put(IS_USER_LOGIN, true);
        Hawk.put(USER_TOKEN, userToken);
        LogUtils.d("isUserLogin :" + isUserLogin + "\n" + " model :" + model + "\n" + Hawk.get(USER_CACHE));
    }

    /**
     * 用户退出登陆
     */
    public void onUserExit() {

        isUserLogin = false;
        model = null;
        Hawk.put(IS_USER_LOGIN, false);
        Hawk.delete(USER_CACHE);
        Hawk.delete(USER_TOKEN);
        Hawk.delete(MyConfig.SP_CACHE_SESSION);
        Hawk.delete(MyConfig.SP_CACHE_TOKEN);
        Hawk.delete(MyConfig.SP_CACHE_HEADER);
        Hawk.delete(MyConfig.SP_CACHE_USERMODEL);

        LogUtils.d("isUserLogin :" + isUserLogin + "\n" + " model :" + model + "\n" + Hawk.get(USER_CACHE));
    }

    /**
     * 判断是否登陆
     *
     * @return
     */
    public boolean isUserLogin() {

        isUserLogin = Hawk.get(IS_USER_LOGIN, false);
        return isUserLogin && model != null;
    }
}
